﻿using KhoaLuan.Areas.Admin.ActionFilters;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace KhoaLuan.Areas.Admin.Controllers
{
    [Security]
    public class ProductController : Controller
    {
        //
        // GET: /Admin/Product/
        LuanVanEntities db = new LuanVanEntities();
        int PageSize = 10;
        public ActionResult GetPage(int PageNo = 0)
        {
            ViewBag.Products = db.Products.ToList()
                .Skip(PageSize * PageNo).Take(PageSize);
            return PartialView("_Product");
        }

        public ActionResult Index()
        {
            ViewBag.PageCount = Math.Ceiling(1.0 * db.Products.ToList().Count / PageSize);
            // Cung cấp cho form
            var model = new Product();
            // Cung cấp cho bảng
            ViewBag.Products = db.Products;
            // Cung cấp cho 2 combo-box
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "NameVN");
            ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "Name");

            return View(model);
        }

        [ValidateInput(false)]
        
    
        public ActionResult Insert(Product model)
        {
            var f = Request.Files["UpImage"];
            if (!String.IsNullOrEmpty(f.FileName))
            {
                f.SaveAs(Server.MapPath("~/images/products/" + f.FileName));
                model.Image = f.FileName;
            }
            try
            {
                db.Products.Add(model);
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
                ViewBag.Products = db.Products;
                // Cung cấp cho 2 combo-box
                ViewBag.CategoryId = new SelectList(db.Categories, "Id", "NameVN");
                ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "Name");
                return View("Index", model);
            }
        }

        [ValidateInput(false)]
        public ActionResult Update(Product model)
        {
            var f = Request.Files["UpImage"];
            if (!String.IsNullOrEmpty(f.FileName))
            {
                System.IO.File.Delete(Server.MapPath("~/images/products/" + model.Image));
                f.SaveAs(Server.MapPath("~/images/products/" + f.FileName));
                model.Image = f.FileName;
            }
            try
            {
                db.Entry(model).State = EntityState.Modified;
                db.SaveChanges();
                ModelState.AddModelError("", "Cập nhật thành công !");
            }
            catch (Exception ex)
            {
                ModelState.AddModelError("", ex.Message);
            }
            ViewBag.Products = db.Products;
            // Cung cấp cho 2 combo-box
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "NameVN", model.CategoryId);
            ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "Name", model.SupplierId);
            return View("Index", model);
        }

        public ActionResult Delete(int Id)
        {
            var model = db.Products.Find(Id);
            db.Products.Remove(model);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        public ActionResult Edit(int Id)
        {
            var model = db.Products.Find(Id);
            ViewBag.Products = db.Products;
            // Cung cấp cho 2 combo-box
            ViewBag.CategoryId = new SelectList(db.Categories, "Id", "NameVN", model.CategoryId);
            ViewBag.SupplierId = new SelectList(db.Suppliers, "Id", "Name", model.SupplierId);
            return View("Index", model);
        }
	}
}